package com.tzpay.tzpay.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.tzpay.tzpay.R;
import com.tzpay.tzpay.adapter.HomeAdapter;
import com.tzpay.tzpay.bean.FunctionInfoWrapper;
import com.tzpay.tzpay.common.ConstValue;
import com.tzpay.tzpay.common.PayType;
import com.tzpay.tzpay.ui.BaseFragment;
import com.tzpay.tzpay.ui.activity.AcquiringActivity;
import com.tzpay.tzpay.ui.activity.TradeListActivity;
import com.tzpay.tzpay.utils.Utils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenfeiyue on 16/3/3.
 */
public class HomeFragment extends BaseFragment {
    public static final String TAG = HomeFragment.class.getSimpleName();

    @Bind(R.id.id_recyclerview)
    RecyclerView recyclerView;

    private HomeAdapter adapter;

    private ArrayList<FunctionInfoWrapper.ItemsEntity> functionInfos = new ArrayList<>();

    public static HomeFragment instance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_app_list, null);
        ButterKnife.bind(this, view);
        initRecycleView();
        initData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.setOnResume(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (adapter != null) {
            adapter.setOnResume(false);
        }
    }

    @OnClick(R.id.rl_scan)
    public void onRlScanClick() {
        startAcquiringActivity(PayType.PAY_SCAN);
    }

    @OnClick(R.id.rl_trade_list)
    public void onRlBarCodeClick() {
        startActivity(getActivity(), TradeListActivity.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void startAcquiringActivity(int payType) {
        Intent intent = new Intent(getActivity(), AcquiringActivity.class);
        intent.putExtra(PayType.PAY_YEPE, payType);
        startActivity(intent);
    }

    private void initRecycleView() {

        adapter = new HomeAdapter(getActivity(), functionInfos);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), ConstValue.COLUMN);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return adapter.getSpanSizeLookup(position);
            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        String json = Utils.getFromAssets(getActivity(), "func.json");
        if (!TextUtils.isEmpty(json)) {
            try {
                Gson gson = new Gson();
                FunctionInfoWrapper infos = gson.fromJson(json, FunctionInfoWrapper.class);
                if (infos != null) {
                    adapter.addData(infos.getItems());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {

            int position = parent.getChildAdapterPosition(view);
            outRect.left = space;
            outRect.right = space;
            outRect.bottom = space;

            // Add top margin only for the first item to avoid double space between items
//            if(parent.getChildPosition(view) == 0)
            outRect.top = space;
        }
    }
}
